package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.hjq.base.BaseDialog;
import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.CreateSceneResult;
import com.qiantu.api.entity.HouseBean;
import com.qiantu.api.entity.MemberBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.entity.SceneDetailBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.dialog.ConfirmHintDialog;
import com.qiantu.phone.utils.SceneDetailUtil;
import com.qiantu.phone.utils.TempDataHolder;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public final class ScenePrivateActivity extends AppActivity {

    private View privateSceneHintView;
//    private View householderLayout;
//    private View houseMemberLayout;
//    private SwitchButton btnHouseMemberSwitch;
//    private SwitchButton btnHouseholderSwitch;
//    private View btnHouseMemberTask;
//    private View btnHouseholderTask;
//    private View btnMemberPrivateSceneManager;
    private String mainSceneSerialNo;
    private SceneBean privateScene;
    private SceneDetailBean privateSceneDetail;
    private SwitchButton btnSwitch;
    private View btnTask;

    @Log
    public static void start(Context context) {
        Intent intent = new Intent(context, ScenePrivateActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scene_private;
    }

    @Override
    protected void initView() {
        privateSceneHintView = findViewById(R.id.private_scene_hint);
        View householderLayout = findViewById(R.id.householder_layout);
//        View houseMemberLayout = findViewById(R.id.house_member_layout);
//        SwitchButton btnHouseMemberSwitch = findViewById(R.id.btn_house_member_switch);
        SwitchButton btnHouseholderSwitch = findViewById(R.id.btn_householder_switch);
//        View btnHouseMemberTask = findViewById(R.id.btn_house_member_task);
        View btnHouseholderTask = findViewById(R.id.btn_householder_task);
        View btnMemberPrivateSceneManager = findViewById(R.id.btn_member_private_scene_manager);
        mainSceneSerialNo = getString("sceneSerialNo");
        String userSerialNo = "";
        if(AppApplication.getInstance().getLoginInfo() != null
                && AppApplication.getInstance().getLoginInfo().getUserInfo() != null
                && AppApplication.getInstance().getLoginInfo().getUserInfo().getUserSerialNo() != null){
            userSerialNo = AppApplication.getInstance().getLoginInfo().getUserInfo().getUserSerialNo();
        }
        privateScene = LLSceneDBManager.getInstance(this).getScenesPrivateBySerialNo(mainSceneSerialNo, userSerialNo);
        HouseBean houseBean = AppApplication.getInstance().getCurHouseInfo();
        if(houseBean != null){
            if(houseBean.getRoleType() == 0){//户主
//                householderLayout.setVisibility(View.VISIBLE);
//                houseMemberLayout.setVisibility(View.GONE);
                btnSwitch = btnHouseholderSwitch;
                btnTask = btnHouseholderTask;
                householderLayout.setVisibility(View.VISIBLE);
                btnMemberPrivateSceneManager.setVisibility(View.VISIBLE);
            }else{//管理员
//                householderLayout.setVisibility(View.GONE);
//                houseMemberLayout.setVisibility(View.VISIBLE);
//                btnSwitch = btnHouseMemberSwitch;
//                btnTask = btnHouseMemberTask;
                btnSwitch = btnHouseholderSwitch;
                btnTask = btnHouseholderTask;
                householderLayout.setVisibility(View.VISIBLE);
                btnMemberPrivateSceneManager.setVisibility(View.GONE);
            }
        }
//        setOnClickListener(btnMemberPrivateSceneManager, btnHouseholderSwitch, btnHouseMemberSwitch, btnHouseholderTask, btnHouseMemberTask);
        setOnClickListener(btnMemberPrivateSceneManager, btnHouseholderSwitch, btnHouseholderTask);
    }

    @Override
    protected void initData() {
        if(privateScene != null){
            privateSceneHintView.setVisibility(View.GONE);
            btnTask.setVisibility(View.VISIBLE);
            LLHttpManager.getSceneDetailInfo(this, privateScene.getSceneSerialNo(), new HttpCallback<HttpData<List<SceneDetailBean>>>(null) {

                @Override
                public void onStart(Call call) {
                    super.onStart(call);
                    showDialog();
                }

                @Override
                public void onSucceed(HttpData<List<SceneDetailBean>> result) {
                    super.onSucceed(result);
                    privateSceneDetail = result.getData().get(0);
                    updateView(privateSceneDetail == null || privateSceneDetail.isDisable());
                }

                @Override
                public void onFail(Exception e) {
                    super.onFail(e);
                }

                @Override
                public void onEnd(Call call) {
                    super.onEnd(call);
                    hideDialog();
                }
            });
        }else{
            privateSceneHintView.setVisibility(View.VISIBLE);
            btnTask.setVisibility(View.GONE);
            updateView(true);
        }
    }

    private void updateView(boolean isDisable){
        if(isDisable){
            btnSwitch.setChecked(false);
        }else{
            btnSwitch.setChecked(true);
            btnTask.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_member_private_scene_manager:
                privateSceneManager();
                break;
            case R.id.btn_householder_switch:
//            case R.id.btn_house_member_switch:
                btnSwitch.setChecked(!btnSwitch.isChecked());
                //启用发送false， 停用发送true
                sceneIsDisable(btnSwitch.isChecked());
                break;
            case R.id.btn_householder_task:
//            case R.id.btn_house_member_task:
                if(privateSceneDetail != null){
                    TempDataHolder.setEditSceneDetailBean(privateSceneDetail);
                    SceneDetailUtil.getDeviceCustomKeyListAndToSceneEdit(privateSceneDetail, this, this,null);
                }else if(privateScene != null){
                    SceneDetailUtil.getSceneDetailAndToEdit(this, this, privateScene.getSceneSerialNo());
                }
                break;
        }
    }

    private void privateSceneManager(){
        LLHttpManager.getHouseMembers(this, AppApplication.getInstance().getCurrentHouseSerialNo(), new HttpCallback<HttpData<List<MemberBean>>>(this) {
            @Override
            public void onSucceed(HttpData<List<MemberBean>> result) {
                if(result.getData() == null || result.getData().size() == 0){
                    showEmptyMemberDialog();
                    return;
                }
                Intent intent = new Intent(ScenePrivateActivity.this, ScenePrivateMemberManagerActivity.class);
                intent.putExtra("mainSceneSerialNo",mainSceneSerialNo);
                startActivity(intent);
            }
        });
    }

    private BaseDialog mConfirmHintDialog;
    private ConfirmHintDialog.Builder mConfirmHintDialogBuilder;
    private void showEmptyMemberDialog() {
        if (mConfirmHintDialog == null) {
            mConfirmHintDialogBuilder = new ConfirmHintDialog.Builder(getActivity())
                    .setContent(getString(R.string.add_house_member_hint))
                    .setRightBtnText(getString(R.string.set_now))
                    .setLeftBtnText(getString(R.string.exit))
                    .setListener(new ConfirmHintDialog.OnListener() {
                        @Override
                        public void onLeftClick(BaseDialog dialog) {
                            dialog.dismiss();
                        }

                        @Override
                        public void onConfirm(BaseDialog dialog) {
                            dialog.dismiss();
                            startActivity(MemberManagerActivity.class);
                        }
                    });
            mConfirmHintDialog = mConfirmHintDialogBuilder.create();
        }
        mConfirmHintDialog.show();
    }

    private void sceneIsDisable(boolean isDisable){
        if(privateScene == null){
            SceneDetailBean addScene = new SceneDetailBean();
            addScene.setBingSceneSerialNo(mainSceneSerialNo);
            addScene.setHouseSerialNo(AppApplication.getInstance().getCurHouseInfo().getHouseSerialNo());
            addScene.setSceneType(4);
            addScene.setDisable(true);
            SceneDetailBean mainSceneDetailBean = TempDataHolder.getEditSceneDetailBean(1);
            if(mainSceneDetailBean != null){
                addScene.setSceneTasks(new ArrayList<>(mainSceneDetailBean.getSceneTasks()));
            }
            addScene.setName(mainSceneDetailBean.getName());
            addScene.setImage(mainSceneDetailBean.getImage());
            addScene.setRoomSerialNo(mainSceneDetailBean.getRoomSerialNo());
            LLHttpManager.sceneCreate(this, addScene, new HttpCallback<HttpData<CreateSceneResult>>(this){
                @Override
                public void onSucceed(HttpData<CreateSceneResult> result) {
                    super.onSucceed(result);
                    privateScene = result.getData().getCreateSceneDto();
                    LLSceneDBManager.getInstance(getContext()).put(privateScene);
                    updateView(isDisable);
                    privateSceneHintView.setVisibility(View.GONE);
                }
            });
        }else {
            LLHttpManager.sceneIsDisable(this, privateScene.getSceneSerialNo(), isDisable, new HttpCallback<HttpData<Void>>(null) {
                @Override
                public void onStart(Call call) {
                    showDialog();
                }

                @Override
                public void onSucceed(HttpData<Void> result) {
                    updateView(isDisable);
                }

                @Override
                public void onFail(Exception e) {
                }

                @Override
                public void onEnd(Call call) {
                    hideDialog();
                }
            });
        }
    }
}